package com.cke.yang.security.domain.repository.impl;

import com.cke.yang.security.domain.entity.SecMenu;
import com.cke.yang.security.domain.entity.SecRoleRelationMenu;
import com.cke.yang.security.domain.repository.SecMenuRepository;
import com.cke.yang.security.infra.mapper.SecMenuMapper;
import com.cke.yang.security.infra.mapper.SecRoleRelationMenuMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 菜单Repository实现类
 *
 * @author yuyang
 */
@Slf4j
@Repository
public class SecMenuRepositoryImpl implements SecMenuRepository {

    @Resource
    private SecMenuMapper secMenuMapper;

    @Resource
    private SecRoleRelationMenuMapper secRoleRelationMenuMapper;

    @Override
    public List<SecMenu> getMenuListByRoleId(Long roleId) {
        log.debug("根据角色ID获取菜单列表, roleId: {}", roleId);

        if (roleId == null) {
            log.warn("角色ID为空，返回空列表");
            return List.of();
        }

        // 1. 根据角色ID查询角色菜单关联关系
        LambdaQueryWrapper<SecRoleRelationMenu> relationQueryWrapper = new LambdaQueryWrapper<>();
        relationQueryWrapper.eq(SecRoleRelationMenu::getRoleId, roleId);
        List<SecRoleRelationMenu> roleMenuRelations = secRoleRelationMenuMapper.selectList(relationQueryWrapper);

        if (roleMenuRelations.isEmpty()) {
            log.debug("角色ID: {} 没有关联任何菜单", roleId);
            return List.of();
        }

        // 2. 提取菜单ID列表
        List<Long> menuIds = roleMenuRelations.stream()
                .map(SecRoleRelationMenu::getMenuId)
                .collect(Collectors.toList());

        // 3. 根据菜单ID列表查询菜单信息，并且只查询启用的菜单
        LambdaQueryWrapper<SecMenu> menuQueryWrapper = new LambdaQueryWrapper<>();
        menuQueryWrapper.in(SecMenu::getMenuId, menuIds);
        // 只查询启用的菜单
        menuQueryWrapper.eq(SecMenu::getIsEnabled, 1);
        List<SecMenu> menus = secMenuMapper.selectList(menuQueryWrapper);

        log.debug("角色ID: {} 关联的菜单数量: {}", roleId, menus.size());
        return menus;
    }

    @Override
    public SecMenu getMenuByMenuCode(String menuCode) {
        log.debug("根据菜单编码获取菜单, menuCode: {}", menuCode);

        if (menuCode == null || menuCode.trim().isEmpty()) {
            log.warn("菜单编码为空，返回null");
            return null;
        }

        LambdaQueryWrapper<SecMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SecMenu::getMenuCode, menuCode);
        // 只查询启用的菜单
        queryWrapper.eq(SecMenu::getIsEnabled, 1);

        SecMenu menu = secMenuMapper.selectOne(queryWrapper);
        log.debug("菜单编码: {} 对应的菜单: {}", menuCode, menu != null ? menu.getMenuName() : "未找到");
        return menu;
    }

    @Override
    public List<SecMenu> getMenuListByMenuCodes(Set<String> menuCodes) {
        log.debug("批量根据菜单编码获取菜单列表, menuCodes: {}", menuCodes);

        if (menuCodes == null || menuCodes.isEmpty()) {
            log.warn("菜单编码集合为空，返回空列表");
            return List.of();
        }

        LambdaQueryWrapper<SecMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SecMenu::getMenuCode, menuCodes);
        // 只查询启用的菜单
        queryWrapper.eq(SecMenu::getIsEnabled, 1);

        List<SecMenu> menus = secMenuMapper.selectList(queryWrapper);
        log.debug("菜单编码集合: {} 对应的菜单数量: {}", menuCodes, menus.size());
        return menus;
    }

}
